﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using ConsoleRedirection;


namespace KentekenHerkenning
{
    public partial class LicencePlateForm : Form
    {
        TextWriter _writer = null;
        public LicencePlateForm()
        {
            InitializeComponent();
        }

        private void openToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "Pictures\\";
            openFileDialog.FileName = "";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                String filename = openFileDialog.FileName;
                srcPictureBox.Image = Image.FromFile(filename);
                Console.Write("Picture has been opened\n");
            }
        }

        private void srcPictureBox_DoubleClick(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.InitialDirectory = "Pictures\\";
            openFileDialog.FileName = "";
            if (openFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                String filename = openFileDialog.FileName;
                srcPictureBox.Image = Image.FromFile(filename);
            }
        } 

        private void Form1_Load(object sender, EventArgs e)
        {
            // Instantiate the writer
            _writer = new TextBoxStreamWriter(consoleTextBox);

            // Redirect the out Console stream
            Console.SetOut(_writer);

            Console.WriteLine("Welcome to the Licence Plate Recognition Program!");
        }

        private void peopeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string dirString = null;
            List<TestsetCharacter> testSet = null;
            List<Bitmap> ImageList = new List<Bitmap>();

            List<String> Images;
            DirectoryInfo Di;
            FolderBrowserDialog FBD = new FolderBrowserDialog();
            if (dirString == null)
            {
                if (FBD.ShowDialog() == DialogResult.OK)
                {

                    dirString = FBD.SelectedPath;

                }
                else
                {
                    return;
                }
            }
            Images = Directory.GetFiles(dirString, "*.*", SearchOption.AllDirectories).ToList();
            foreach (string @s in Images)
            {
                Di = new DirectoryInfo(@s);
                string name = s.Substring(dirString.Length + 1);

                Bitmap newBitmap = new Bitmap(Image.FromFile(@s));
                newBitmap = newBitmap.addWhiteEdge(5);
                newBitmap.Tag = name;


                ImageList.Add(newBitmap);

            }


            testSet = Awesome_O_CR.readTestSet(ImageList);

            if (testSet != null)
            {
                try
                {
                    testSet.ObjectToFile("CompareSet.OCR");
                    Console.WriteLine("Save succesvol");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Save gefaald::::::\n" + ex);
                }
            }
            else
            {
                Console.WriteLine("Kan niet saven, TestSet is leeg");
            }
        }

        private void algo1ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Licenseplate recognition has been started");
            srcPictureBox.Image = ((Bitmap)LicensePlateRecognition.startAlgo1((Bitmap)srcPictureBox.Image));

            //output here
        }

        private void algo2ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Licenseplate recognition has been started");
            srcPictureBox.Image = ((Bitmap)LicensePlateRecognition.startAlgo2((Bitmap)srcPictureBox.Image));

            //output here
        }     
    }
}
